package com.sddz.pacs.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sddz.pacs.dto.PageModelParams;
import com.sddz.pacs.entity.HealthUser;
import com.sddz.pacs.entity.SysUser;
import com.sddz.pacs.mapper.HealthUserMapper;
import com.sddz.pacs.service.HealthUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sddz.pacs.service.SysStationService;
import com.sddz.pacs.util.ShiroUtils;
import com.sddz.pacs.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 健康体检人员信息表 服务实现类
 * </p>
 *
 * @author 时代电子
 * @since 2018-09-10
 */
@Service
public class HealthUserServiceImpl extends ServiceImpl<HealthUserMapper, HealthUser> implements HealthUserService {

     @Autowired
     private SysStationService sysStationService;

    @Autowired
    private HealthUserService healthUserService;
    @Override
    public PageInfo<HealthUser> selectHealthUserList(PageModelParams params) {
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SysUser sysUser= ShiroUtils.getUserEntity();
       EntityWrapper<HealthUser> ew = new EntityWrapper<>();
            ew.eq("STATION_ID", sysUser.getStationId());
        //判断是否有查询条件
        if (params.getCondition()!=null){

            if (StringUtils.isNotEmpty(params.getCondition().get("startDate")) && StringUtils.isNotEmpty(params.getCondition().get("endDate"))){
                try {
                    ew.between("CREATE_DATE",formatter.parse(params.getCondition().get("startDate")+""),formatter.parse(params.getCondition().get("endDate")+""));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }

            if (StringUtils.isNotEmpty(params.getCondition().get("name"))){
                ew.like("NAME",params.getCondition().get("name")+"");
            }
            if (StringUtils.isNotEmpty(params.getCondition().get("idCard"))){
                ew.eq("ID_CARD",params.getCondition().get("idCard"));
            }

        }
        List<HealthUser> ealthUserList =  this.baseMapper.selectList(ew);


        return new PageInfo<>(ealthUserList);
    }

    @Override
    public Map indexNumber(String stationId) {

        return  this.baseMapper.indexNumber(stationId);
    }

    @Override
    public List<HealthUser> getDayData(String stationId) {
        return this.baseMapper.getDayData(stationId);
    }

    @Override
    public Map getYearEveryMonth(String stationId) {
        return this.baseMapper.getYearEveryMonth(stationId);
    }

    @Override
    public List<Map> getSevenDay(String stationId) {
        return this.baseMapper.getSevenDay(stationId);
    }

    @Override
    public Map mangerNumber() {
        return this.baseMapper.mangerNumber();
    }

    @Override
    public Map getByManWomanNum(String stationId) {
        return this.baseMapper.getByManWomanNum(stationId);
    }

    @Override
    public Map getManger() {
        return this.baseMapper.getMangerManWomanNum();
    }

    @Override
    public List<Map<String,Object>> getLastData() {
        return this.baseMapper.countZong();
    }
    //获取桥西区所有居民列表
    @Override
    public PageInfo<HealthUser> selectAllHealthUserList(PageModelParams params) {
      //分页
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前登陆场对象
            SysUser sysUser= ShiroUtils.getUserEntity();
        EntityWrapper<HealthUser> ew = new EntityWrapper<>();
        //判断是否有查询条件
        if (params.getCondition()!=null){
            if (StringUtils.isNotEmpty(params.getCondition().get("startDate")) && StringUtils.isNotEmpty(params.getCondition().get("endDate"))){
                try {
                    ew.between("CREATE_DATE",formatter.parse(params.getCondition().get("startDate")+""),formatter.parse(params.getCondition().get("endDate")+""));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            if (StringUtils.isNotEmpty(params.getCondition().get("name"))){
                ew.like("NAME",params.getCondition().get("name")+"");
            }
            if (StringUtils.isNotEmpty(params.getCondition().get("idCard"))){
                ew.eq("ID_CARD",params.getCondition().get("idCard"));
            }
                if (StringUtils.isNotEmpty(params.getCondition().get("stationId"))){
                    ew.eq("STATION_ID", params.getCondition().get("stationId"));
            }

        }
        List<HealthUser> ealthUserList =  this.baseMapper.selectList(ew);

        return new PageInfo<>(ealthUserList);

    }

    @Override
    public Map<String, Integer> getMapData() {
        return     this.baseMapper.getMapData();
    }



    @Override
    public List<Map> getMangerSevenDay() {
        return this.baseMapper.getMangerSevenDay();
    }

    @Override
    public List<HealthUser> getThreeDays() {
        return this.baseMapper.getThreeDays();
    }

    @Override
    public boolean insertOrUpdateUser(HealthUser user) {

        user.setCreateDate(new Date());
        EntityWrapper<HealthUser> ew = new EntityWrapper<>();
        ew.eq("ID_CARD", user.getIdCard());
        HealthUser healthUser= this.selectOne(ew);
        boolean result=false;


        if(healthUser!=null){
            result=this.update(user,ew);

        }else{
            result = this.insert(user);
        }
        return result;
    }
}
